package Stream流练习;

import java.util.*;
import java.util.function.Consumer;

/**
 * @Date 2024/12/13 14:28
 * @description: stream 流练习
 * .
 * @Author LittleNight
 */
public class StreamDemo1 {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");

        ArrayList<String> list1 = new ArrayList<>();

        for (String name : list) {
            if(name.startsWith("张")) {
                list1.add(name);
            }
        }

        list.forEach(name -> System.out.println(name));

        Map<String, Integer> hash = new HashMap<>();
        hash.put("张无忌", 18);
        hash.put("1周芷若", 19);
        hash.put("3赵敏", 20);

        // 变成了一个 set 集合
        Set<Map.Entry<String, Integer>> entries = hash.entrySet();
        // 底层hash 表，不能保证遍历顺序和存储顺序一样的
//        entries.stream().forEach(s -> System.out.println(s));

        /*entries.stream().forEach(new Consumer<Map.Entry<String, Integer>>() {
            @Override
            public void accept(Map.Entry<String, Integer> entries) {
                System.out.println(entries.getKey());
                System.out.println(entries.getValue());
            }
        });*/
        // 变成 lambda 表达式就是这样的
        entries.stream().forEach(entries1 -> {
            System.out.println(entries1.getKey());
            System.out.println(entries1.getValue());
        });
        entries.stream().map(s -> s.getKey()).forEach(s -> System.out.println(s));
    }

}
